/********************************************************************************************************

Script:			Rename Jobs
Author:			Richard Slim
Date:			17/12/2013
Description:	This script Renames all of the jobs adding DBM - to the front

Usage:			This is called from within the main SSIS task but can also be used manually
				
********************************************************************************************************/

IF EXISTS (SELECT 1 FROM msdb.dbo.sysjobs WHERE name = 'CommandLog Cleanup')
BEGIN
EXEC msdb.dbo.sp_update_job
    @job_name = N'CommandLog Cleanup',
    @new_name = N'DBM - CommandLog Cleanup'
END

IF EXISTS (SELECT 1 FROM msdb.dbo.sysjobs WHERE name = 'DatabaseBackup - SYSTEM_DATABASES - FULL')
BEGIN
EXEC msdb.dbo.sp_update_job
    @job_name = N'DatabaseBackup - SYSTEM_DATABASES - FULL',
    @new_name = N'DBM - DatabaseBackup - SYSTEM_DATABASES - FULL'
END

IF EXISTS (SELECT 1 FROM msdb.dbo.sysjobs WHERE name = 'DatabaseBackup - USER_DATABASES - DIFF')
BEGIN
EXEC msdb.dbo.sp_update_job
    @job_name = N'DatabaseBackup - USER_DATABASES - DIFF',
    @new_name = N'DBM - DatabaseBackup - USER_DATABASES - DIFF'
END

IF EXISTS (SELECT 1 FROM msdb.dbo.sysjobs WHERE name = 'DatabaseBackup - USER_DATABASES - FULL')
BEGIN
EXEC msdb.dbo.sp_update_job
    @job_name = N'DatabaseBackup - USER_DATABASES - FULL',
    @new_name = N'DBM - DatabaseBackup - USER_DATABASES - FULL'
END

IF EXISTS (SELECT 1 FROM msdb.dbo.sysjobs WHERE name = 'DatabaseBackup - USER_DATABASES - LOG')
BEGIN
EXEC msdb.dbo.sp_update_job
    @job_name = N'DatabaseBackup - USER_DATABASES - LOG',
    @new_name = N'DBM - DatabaseBackup - USER_DATABASES - LOG'
END

IF EXISTS (SELECT 1 FROM msdb.dbo.sysjobs WHERE name = 'DatabaseIntegrityCheck - SYSTEM_DATABASES')
BEGIN
EXEC msdb.dbo.sp_update_job
    @job_name = N'DatabaseIntegrityCheck - SYSTEM_DATABASES',
    @new_name = N'DBM - DatabaseIntegrityCheck - SYSTEM_DATABASES'
END

IF EXISTS (SELECT 1 FROM msdb.dbo.sysjobs WHERE name = 'DatabaseIntegrityCheck - USER_DATABASES')
BEGIN
EXEC msdb.dbo.sp_update_job
    @job_name = N'DatabaseIntegrityCheck - USER_DATABASES',
    @new_name = N'DBM - DatabaseIntegrityCheck - USER_DATABASES'
END

IF EXISTS (SELECT 1 FROM msdb.dbo.sysjobs WHERE name = 'IndexOptimize - USER_DATABASES')
BEGIN
EXEC msdb.dbo.sp_update_job
    @job_name = N'IndexOptimize - USER_DATABASES',
    @new_name = N'DBM - IndexOptimize - USER_DATABASES'
END

IF EXISTS (SELECT 1 FROM msdb.dbo.sysjobs WHERE name = 'Output File Cleanup')
BEGIN
EXEC msdb.dbo.sp_update_job
    @job_name = N'Output File Cleanup',
    @new_name = N'DBM - Output File Cleanup'
END

IF EXISTS (SELECT 1 FROM msdb.dbo.sysjobs WHERE name = 'sp_delete_backuphistory')
BEGIN
EXEC msdb.dbo.sp_update_job
    @job_name = N'sp_delete_backuphistory',
    @new_name = N'DBM - sp_delete_backuphistory'
END

IF EXISTS (SELECT 1 FROM msdb.dbo.sysjobs WHERE name = 'sp_purge_jobhistory')
BEGIN
EXEC msdb.dbo.sp_update_job
    @job_name = N'sp_purge_jobhistory',
    @new_name = N'DBM - sp_purge_jobhistory'
END

IF EXISTS (SELECT 1 FROM msdb.dbo.sysjobs WHERE name = 'syspolicy_purge_history')
BEGIN
EXEC msdb.dbo.sp_update_job
    @job_name = N'syspolicy_purge_history',
    @new_name = N'DBM - syspolicy_purge_history'
END


